
/**
 * @FileName AssertUtil.java
 * @Desciption TODO
 * @author zhouchuanbo
 * @date 2019年9月24日
 */
package com.dotflat.dashboard.util;

import com.dotflat.dashboard.vo.*;
import com.matt.commons.dto.sso.AreaDTO;
import com.matt.domain.Children;
import com.matt.service.remoteCall.DTO.AreaStaffDTO;
import com.matt.service.remoteCall.DTO.SsoAreaDTO;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;

import java.text.NumberFormat;
import java.util.*;


public class PanelUtil {


    public static  List<StaffCountVO> getStaffCountVO(List<SsoAreaDTO> areaDTOList, List<AreaStaffDTO> areaStaffList, Integer groupCount) {

        List<StaffCountVO> list = new ArrayList<>();

        for (int i = 0; i < areaDTOList.size() ; i++) {

            StaffCountVO staffCountVO = new StaffCountVO();
            staffCountVO.setAreaId(areaDTOList.get(i).getId());
            staffCountVO.setAreaName(areaDTOList.get(i).getAreaName());
            staffCountVO.setLogo(areaDTOList.get(i).getLogo());
            String areaId = areaDTOList.get(i).getId();
            int count = 0;
            for (int j = 0; j < areaStaffList.size(); j++) {
                if (StringUtils.equals(areaId,areaStaffList.get(j).getAreaId())){
                    count++;
                }
            }
            NumberFormat numberFormat = NumberFormat.getInstance();
            String proportion = numberFormat.format((float)count  / (float) groupCount * 100) + "%";
            staffCountVO.setProportion(proportion);
            staffCountVO.setStaffCount(count);
            staffCountVO.setTeacherCount(count);
            list.add(staffCountVO);
        }
        return list;
    }


    public static  List<ChildrenCountVO> getChildrenCountVO(List<SsoAreaDTO> areaDTOList, List<Children> childrenList, Integer groupCount) {
        List<ChildrenCountVO> list = new ArrayList<>();
        for (int i = 0; i < areaDTOList.size() ; i++) {

            ChildrenCountVO childrenCountVO = new ChildrenCountVO();
            childrenCountVO.setAreaId(areaDTOList.get(i).getId());
            childrenCountVO.setAreaName(areaDTOList.get(i).getAreaName());
            String areaId = areaDTOList.get(i).getId();
            int count = 0;
            //男 - 儿童总数
            int maleCount =0;
            //女 - 儿童总数
            int femaleCount=0;
            for (int j = 0; j < childrenList.size(); j++) {
                if (StringUtils.equals(areaId,childrenList.get(j).getAreaId())){
                    count++;
                    if (childrenList.get(j).getGender() == 0){
                        femaleCount++;
                    }else {
                        maleCount++;
                    }
                }
            }
            NumberFormat numberFormat = NumberFormat.getInstance();
            String proportion = numberFormat.format((float)count  / (float) groupCount * 100) + "%";
            childrenCountVO.setProportion(proportion);
            childrenCountVO.setChildrenCount(count);
            childrenCountVO.setFemaleCount(femaleCount);
            childrenCountVO.setMaleCount(maleCount);
            list.add(childrenCountVO);
        }
        return list;
    }

    public static  List<Map> getAreaCount(List<SsoAreaDTO> areaDTOList, List<Map>  list1,  List<ChildrenCountVO> countVOList,    List<StaffCountVO> staffCountVOList) {
        List<AreaCountVO> areaCountVOList = new ArrayList<>();
        for (int i = 0; i < areaDTOList.size() ; i++) {
            AreaCountVO areaCountVO = new AreaCountVO();
            areaCountVO.setAreaName(areaDTOList.get(i).getAreaName());
            areaCountVO.setAreaId(areaDTOList.get(i).getId());
            areaCountVO.setArea(areaDTOList.get(i).getPcaCodeDesc());
            areaCountVO.setLogo(areaDTOList.get(i).getLogo());
            if (StringUtils.equals("0E-7",areaDTOList.get(i).getLatitude())){
                areaCountVO.setLatitude("0");
            }else {
                areaCountVO.setLatitude(areaDTOList.get(i).getLatitude());
            }
            if (StringUtils.equals("0E-7",areaDTOList.get(i).getLongtitude())){
                areaCountVO.setLongtitude("0");
            }else {
                areaCountVO.setLongtitude(areaDTOList.get(i).getLongtitude());
            }


            for (int j = 0; j < list1.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),list1.get(j).get("areaId").toString())){
                    areaCountVO.setClassesCount(Integer.parseInt(list1.get(j).get("count").toString()));
                }
            }
            for (int j = 0; j < countVOList.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),countVOList.get(j).getAreaId())){
                    areaCountVO.setChildrenCount(countVOList.get(j).getChildrenCount());
                    areaCountVO.setProportion(countVOList.get(j).getProportion());
                }
            }
            for (int j = 0; j < staffCountVOList.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),staffCountVOList.get(j).getAreaId())){
                    areaCountVO.setStaffCount(staffCountVOList.get(j).getStaffCount());
                    areaCountVO.setTeacherCount(staffCountVOList.get(j).getTeacherCount());
                }
            }
            areaCountVOList.add(areaCountVO);
        }
        List<Map> mapList = new ArrayList<>();
        for (int i = 0; i < areaCountVOList.size() ; i++) {
            if (Objects.isNull(areaCountVOList.get(i).getArea())){
                continue;
            }
            Map map = new HashMap();
            if (getIsArea(mapList,areaCountVOList.get(i).getArea())){
                continue;
            }
            map.put("area",areaCountVOList.get(i).getArea());
            List<AreaCountVO> areaCountVOList1 = new ArrayList<>();
            for (int j = 0; j < areaCountVOList.size(); j++) {
                if (StringUtils.equals(areaCountVOList.get(i).getArea(),areaCountVOList.get(j).getArea())){
                  if (getIsAreaId(areaCountVOList1,areaCountVOList.get(j).getAreaId())){
                      areaCountVOList1.add(areaCountVOList.get(j));
                  }
                }
            }
            map.put("areaCount",areaCountVOList1);
            mapList.add(map);
        }
        return  mapList;
    }

    /**
     * 是否包含该id
     * @param areaCountVOList
     * @param areaId
     * @return
     */
    public static boolean getIsAreaId(List<AreaCountVO> areaCountVOList,String areaId){

        for (int i = 0; i < areaCountVOList.size(); i++) {
            if (Objects.isNull(areaCountVOList.get(i).getAreaId()) || Objects.isNull(areaId)){
                return false;
            }
            if (StringUtils.equals(areaCountVOList.get(i).getAreaId(),areaId)){
                return false;
            }
        }
        return true;
    }

    /**
     * 是否包含该id
     * @return
     */
    public static boolean getIsArea(List<Map> mapList,String area){
        for (int i = 0; i < mapList.size(); i++) {
            if (Objects.isNull(mapList.get(i).get("area")) || Objects.isNull(area)){
                return true;
            }
            if (StringUtils.equals(mapList.get(i).get("area").toString(),area)){
                return true;
            }
        }
        return false;
    }


    public static  List<AreaAllVO> getAreaAll(List<SsoAreaDTO> areaDTOList, List<Map>  list1, List<ChildrenCountVO> countVOList, List<StaffCountVO> staffCountVOList,  List<AreaClassTypeCountVO>  areaClassTypeCount) {
        List<AreaAllVO> areaCountVOList = new ArrayList<>();
        for (int i = 0; i < areaDTOList.size() ; i++) {
            AreaAllVO areaCountVO = new AreaAllVO();
            areaCountVO.setAreaName(areaDTOList.get(i).getAreaName());
            areaCountVO.setAreaId(areaDTOList.get(i).getId());
            areaCountVO.setArea(areaDTOList.get(i).getPcaCodeDesc());
            areaCountVO.setLogo(areaDTOList.get(i).getLogo());
            areaCountVO.setAreaCover(areaDTOList.get(i).getAreaCover());

            if (StringUtils.equals("0E-7",areaDTOList.get(i).getLatitude())){
                areaCountVO.setLatitude("0");
            }else {
                areaCountVO.setLatitude(areaDTOList.get(i).getLatitude());
            }
            if (StringUtils.equals("0E-7",areaDTOList.get(i).getLongtitude())){
                areaCountVO.setLongtitude("0");
            }else {
                areaCountVO.setLongtitude(areaDTOList.get(i).getLongtitude());
            }


            for (int j = 0; j < list1.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),list1.get(j).get("areaId").toString())){
                    areaCountVO.setClassesCount(Integer.parseInt(list1.get(j).get("count").toString()));
                }
            }
            for (int j = 0; j < countVOList.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),countVOList.get(j).getAreaId())){
                    areaCountVO.setChildrenCount(countVOList.get(j).getChildrenCount());
                    areaCountVO.setProportion(countVOList.get(j).getProportion());
                    areaCountVO.setFemaleCount(countVOList.get(i).getFemaleCount());
                    areaCountVO.setMaleCount(countVOList.get(i).getMaleCount());
                }
            }
            for (int j = 0; j < staffCountVOList.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),staffCountVOList.get(j).getAreaId())){
                    areaCountVO.setStaffCount(staffCountVOList.get(j).getStaffCount());
                    areaCountVO.setTeacherCount(staffCountVOList.get(j).getTeacherCount());
                }
            }

            for (int j = 0; j < areaClassTypeCount.size(); j++) {
                if (StringUtils.equals(areaDTOList.get(i).getId(),areaClassTypeCount.get(j).getAreaId())){
                    areaCountVO.setAreaClassTypeCount(areaClassTypeCount.get(j));
                }
            }
            areaCountVOList.add(areaCountVO);
        }
        return  areaCountVOList;
    }

}
